package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.PhotoAlbum;
import com.cz.zslu.entity.PhotoAttachment;
import com.cz.zslu.entity.SysUser;
import com.cz.zslu.entity.ZslStoreCommodity;
import com.cz.zslu.interceptor.BaseContextHandler;
import com.cz.zslu.interceptor.IgnoreAuth;
import com.cz.zslu.rest.BaseController;
import com.cz.zslu.service.PhotoAlbumService;
import com.cz.zslu.service.ISysUserService;
import com.cz.zslu.service.PhotoAttachmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 相册管理 前端控制器
 * </p>
 *
 * @author Bruce
 * @since 2019-06-10
 */
@RestController
@RequestMapping("/sys/photoalbum")
@Api(description = "相册管理")
public class PhotoAlbumController extends BaseController<PhotoAlbumService, PhotoAlbum> {

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private PhotoAttachmentService photoAttachmentService;

    @ApiOperation("获取该店铺下全部相册分页")
//    @IgnoreAuth
    @RequestMapping(value = "/getPage", method = RequestMethod.POST)
    public ReturnObject<List<PhotoAlbum>> getPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                  @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                  @ApiParam("storeId") @RequestParam Long storeId) {
        EntityWrapper<PhotoAlbum> ew = new EntityWrapper<>();
        ew.eq("state", 0);
        ew.eq("store_id", storeId);
        ew.orderBy("crt_time", false);
        Page<PhotoAlbum> obj = baseService.selectPage(new Page<>(offset, limit), ew);
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }

    @ApiOperation("通过id获取详情")
//    @IgnoreAuth
    @RequestMapping(value = "/findInfoById", method = RequestMethod.POST)
    public ReturnObject<PhotoAlbum> findInfoById(@ApiParam("id") @RequestParam Long id) {

        return new ReturnObject<>(0, "获取成功", baseService.selectById(id));
    }


    @ApiOperation("新增/编辑（新增时：id为空）")
    @RequestMapping(value = "/savePhotoAlbum", method = RequestMethod.POST)
    public ReturnObject<PhotoAlbum> savePhotoAlbum(@ApiParam("storeId") @RequestParam Long storeId,
                                                   @ApiParam("相册标题") @RequestParam String name,
                                                   @ApiParam("相册描述") @RequestParam String content,
                                                   @ApiParam("id") @RequestParam(defaultValue = "0") Long id) {

        if (id != 0) {
            PhotoAlbum info = baseService.selectById(id);
            info.setContent(content);
            info.setName(name);
            baseService.updateById(info);
        } else {
            PhotoAlbum info = new PhotoAlbum();
            info.setStoreId(storeId);
            info.setContent(content);
            info.setName(name);
            baseService.insert(info);
        }

        return new ReturnObject<>(0, "新增成功", null);

    }


    @ApiOperation("删除相册")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ReturnObject<PhotoAlbum> del(@ApiParam("id") @RequestParam Long id) {

        PhotoAlbum photoAlbum = baseService.selectById(id);
        photoAlbum.setState(1);
        baseService.updateById(photoAlbum);
        List<PhotoAttachment> list = photoAttachmentService.selectList(new EntityWrapper<PhotoAttachment>().eq("album_id", id));
        for (PhotoAttachment info : list) {
            info.setState(1);
            photoAttachmentService.updateById(info);

        }

        return new ReturnObject<>(0, "删除成功", null);
    }

    @ApiOperation("相册总容量")
    @RequestMapping(value = "/total", method = RequestMethod.POST)
    public ReturnObject<String> total() {
        SysUser sysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        List<PhotoAlbum> albums = baseService.selectList(new EntityWrapper<PhotoAlbum>().eq("store_id", sysUser.getStoreId()));
       Long capacity=0L;
        for(int i=0;i<albums.size();i++){
            capacity+=albums.get(i).getCapacity();
        }
        DecimalFormat df = new DecimalFormat("0.0");//格式化小数
        //以M为单位
        String storeTotal = df.format((float)capacity/(1024*1024));

        return new ReturnObject<>(0, "获取成功", storeTotal);
    }

    @ApiOperation("添加封面")
    @RequestMapping(value = "/addCover", method = RequestMethod.POST)
    public ReturnObject<PhotoAlbum> addCover(@ApiParam("相册id") @RequestParam Long PhotoAlbum,
                                             @ApiParam("图片id") @RequestParam Long photoId) {

        PhotoAlbum info = baseService.selectById(PhotoAlbum);
        if(info.getCoverId()!=null&&info.getCoverId()!=0){
            PhotoAttachment oldPhotoAttachment=photoAttachmentService.selectById(info.getCoverId());
            if(oldPhotoAttachment.getIsCover()==1){
                oldPhotoAttachment.setIsCover(0);
                photoAttachmentService.updateById(oldPhotoAttachment);
            }
        }
        PhotoAttachment newPhotoAttachment=photoAttachmentService.selectById(photoId);
        newPhotoAttachment.setIsCover(1);
        photoAttachmentService.updateById(newPhotoAttachment);
        info.setCoverId(photoId);
        info.setCoverUrl(newPhotoAttachment.getPhotoUrl());
        baseService.updateById(info);
        return new ReturnObject<>(0, "修改成功", null);
    }

}

